/*
 * Copyright (C) 2014 nohana, Inc.
 * Copyright 2017 Zhihu Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.xiaoyu.media.matisse.internal.entity

import android.content.pm.ActivityInfo
import androidx.annotation.StyleRes
import com.xiaoyu.media.matisse.MimeType
import com.xiaoyu.media.matisse.engine.ImageEngine
import com.xiaoyu.media.matisse.engine.impl.PicassoEngine
import com.xiaoyu.media.matisse.filter.Filter
import com.xiaoyu.media.matisse.listener.OnCheckedListener
import com.xiaoyu.media.matisse.listener.OnSelectedListener
import com.xiaoyu.media.R
import java.util.*

class SelectionSpec private constructor() {

    var mimeTypeSet: MutableSet<MimeType> = HashSet()
    var mediaTypeExclusive: Boolean = false
    var showSingleMediaType: Boolean = false
    @StyleRes
    var themeId: Int = 0
    var orientation: Int = 0
    var countable: Boolean = false
    var maxSelectable = 1
    var maxImageSelectable: Int = 0
    var maxVideoSelectable: Int = 0
    var filters: MutableList<Filter> = ArrayList()
    var capture: Boolean = false
    var captureStrategy: CaptureStrategy? = null
    var spanCount = 3
    var gridExpectedSize: Int = 0
    var thumbnailScale = 0.5f
    var imageEngine: ImageEngine = PicassoEngine()
    var hasInited: Boolean = false
    var onSelectedListener: OnSelectedListener? = null
    var originalable: Boolean = false
    var autoHideToobar: Boolean = false
    var originalMaxSize = Integer.MAX_VALUE
    var onCheckedListener: OnCheckedListener? = null
    var videoMaxDuration = COLLECTION_MAX_VIDEO_DURATION

    private fun reset() {
        mimeTypeSet = HashSet()
        mediaTypeExclusive = true
        showSingleMediaType = false
        themeId = R.style.Matisse_Zhihu
        orientation = 0
        countable = false
        maxSelectable = 1
        maxImageSelectable = 0
        maxVideoSelectable = 0
        filters = ArrayList()
        capture = false
        captureStrategy = null
        spanCount = 3
        gridExpectedSize = 0
        thumbnailScale = 0.5f
        imageEngine = PicassoEngine()
        hasInited = true
        originalable = false
        autoHideToobar = false
        originalMaxSize = Integer.MAX_VALUE
        videoMaxDuration = COLLECTION_MAX_VIDEO_DURATION
    }

    fun singleSelectionModeEnabled(): Boolean {
        return !countable && (maxSelectable == 1 || maxImageSelectable == 1 && maxVideoSelectable == 1)
    }

    fun needOrientationRestriction(): Boolean {
        return orientation != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
    }

    fun onlyShowImages(): Boolean {
        return showSingleMediaType && MimeType.ofImage().containsAll(mimeTypeSet)
    }

    fun onlyShowVideos(): Boolean {
        return showSingleMediaType && MimeType.ofVideo().containsAll(mimeTypeSet)
    }

    fun isLongVideo(item: Item?): Boolean {
        return item?.isVideo == true && item.duration.div(1000) > COLLECTION_MAX_VIDEO_DURATION
    }

    private object InstanceHolder {
        internal val INSTANCE = SelectionSpec()
    }

    companion object {

        val instance: SelectionSpec
            get() = InstanceHolder.INSTANCE

        val cleanInstance: SelectionSpec
            get() {
                val selectionSpec = instance
                selectionSpec.reset()
                return selectionSpec
            }

        const val COLLECTION_MAX_VIDEO_DURATION = 60
    }
}
